package easy.FlippinganImage;

import org.junit.Test;

/*
给定二进制矩阵A，我们想要水平翻转图像，然后反转它，并返回结果图像。

水平翻转图像意味着图像的每一行都是相反的。 例如，水平翻转110会产生011。

反转图像意味着每个0被1替换，每个1被0替换。例如，反转011会产生100。
Example 1:

Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
Example 2:

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
 */
public class Solution {
    public int[][] flipAndInvertImage(int[][] A) {
        int[][]B=new int[A.length][];
        for (int i = 0; i < A.length; i++) {
            int[] row = A[i];
            //reverse 先翻转
            int[] reverse =new int[row.length];
            for (int j = 0; j < row.length; j++) {
                reverse[j]=row[row.length-j-1];
            }
            //取反
            for (int j = 0; j < reverse.length; j++) {
                if (1==reverse[j]){
                    reverse[j]=0;
                }else if (0==reverse[j]){
                    reverse[j]=1;
                }else {
                    throw new IllegalArgumentException("参数错误，检查输入");
                }
            }
            B[i]=reverse;
        }
        return B;
    }
    @Test
    public void test1(){
        Solution solution=new Solution();
        int[][] ints = solution.flipAndInvertImage(new int[][]{{1, 1, 0}, {1, 0, 1}, {0, 0, 0}});
        System.out.println(ints);
        System.out.printf("[");
        for (int i = 0; i < ints.length; i++) {
            System.out.printf("[");
            for (int j = 0; j < ints[i].length; j++) {
                System.out.printf("%d,",ints[i][j]);
            }
            System.out.printf("]");
        }
        System.out.printf("]");

    }
}
